package com.example.benefitcommunity.controller;

import com.example.benefitcommunity.entity.BcUser;
import com.example.benefitcommunity.entity.Result;
import com.example.benefitcommunity.enums.StatusCode;
import com.example.benefitcommunity.service.BcUserService;

import com.example.benefitcommunity.util.UploadFileUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户登录表(BcUser)表控制层
 *
 * @author makejava
 * @since 2025-08-04 09:39:09
 */

@Tag(name = "用户")
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("bcUser")
public class BcUserController {
    /**
     * 服务对象
     */
    @Resource
    private BcUserService bcUserService;

    /**
     * 查询
     *
     * @param username 用户
     * @param password 密码
     * @return 查询结果
     */
    @Operation(description = "用户登录")
    @GetMapping("userLogin")
    public Result<Object> userLogin(String username, String password) {
        return this.bcUserService.queryByUserNameAndPassword(username, password);
    }


    /**
     * 注册
     *
     * @param username 用户名
     * @param phone 手机号
     * @param password 密码
     * @return 注册结果
     */
    @Operation(description = "用户注册")
    @GetMapping("register")
    public Result<Object> register(String username, String phone, String password) {
        return this.bcUserService.register(username, phone ,password);
    }

    /**
     * 忘记密码
     *
     * @param username 用户名
     * @param password 密码
     * @return 忘记密码结果
     */
    @Operation(description = "忘记密码")
    @GetMapping("forgotPassword")
    public Result<Object> forgotPassword(String username,  String password) {
        return this.bcUserService.forgotPassword(username,  password);
    }


    /**
     * 修改个人信息
     *
     * @param bcUser
     * @return 返回
     */
    @Operation(description = "个人信息")
    @PutMapping("personInfo")
    public Result<Object> personInfo(@RequestBody BcUser bcUser) {
        return this.bcUserService.personInfo(bcUser);
    }

    /**
     * 上传文件
     *
     * @param files 文件
     * @return 单条数据
     */
    @Operation(description = "上传附件")
    @PostMapping("uploadFile")
    public Result<Object> uploadFile(@RequestParam ("file")  MultipartFile[] files) {
        //判断参数是否为null
        if (files == null || files.length == 0) {
            return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "上传失败!");
        }
        List<String> fileUrlList = new ArrayList<>();
        for (MultipartFile file : files) {
            log.info("上传文件：{}", file);
            String fileUrl = UploadFileUtil.upload(file);
            fileUrlList.add(fileUrl);
        }
        return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), (long) fileUrlList.size(), fileUrlList);
    }
}

